package com.xxpt.common.auth;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.xxpt.common.constant.SysConstant;
import com.xxpt.common.constant.UserContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录信息解析
 * @Author masl - 2020/4/8 10:59
 */
public interface UserAuthAware {
    @ModelAttribute
    default AuthProperties opProperties(HttpServletRequest request, @RequestHeader(value = UserContext.AUTHORIZE_TOKEN, required = false) String token) {
        try {
            AuthProperties authProperties = null;
            if (StringUtils.isNotBlank(token)) {
                authProperties = JSON.parseObject(SecureUtil.aes(SysConstant.SESSION_AES_KEY.getBytes()).decryptStr(token), AuthProperties.class);
                authProperties.setToken(token);
            }
            //request.setAttribute("authProperties", authProperties);
            return authProperties;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    static void main(String[] args) throws Exception {
        System.out.println(SecureUtil.aes(SysConstant.SESSION_AES_KEY.getBytes())
                .decryptStr("kz/8FRY+lcDOLhdAon5Dz9lPVCI8E4/JMTgukIi0lY4h+6LjP/CXESvexsL0JI+TSxLRt64hQoROQ642Vxd4PA=="));
    }
}
